package com.src;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.Scroller;
import android.widget.Toast;

import com.src.R;

public class ScrollLayout extends ViewGroup {
	private Scroller cmScroller;
	private GestureDetector gestureDetector;
	private float moveWidth;
	private int mWidth;
	private boolean fling = false;
	private Context c;
	private TestFlip aaa;
	private int count = 0;
	private int count1 = 0;
	private String flg = "0";

	public ScrollLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		cmScroller = new Scroller(context);
		gestureDetector = new GestureDetector(context, gestureListener);
		gestureDetector.setIsLongpressEnabled(false);
		c = context;
		aaa = (TestFlip) c;

	}

	private void setMWidth() {
		if (mWidth == 0) {
			mWidth = getWidth();
		}
	}

	@Override
	protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
		int childLeft = 0;

		final int childCount = getChildCount();

		for (int i = 0; i < childCount; i++) {

			final View childView = getChildAt(i);

			if (childView.getVisibility() != View.GONE) {

				final int childWidth = childView.getMeasuredWidth();

				childView.layout(childLeft, 0,

				childLeft + childWidth, childView.getMeasuredHeight());

				childLeft += childWidth;

			}

		}

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		final int width = MeasureSpec.getSize(widthMeasureSpec);

		final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		Log.e("onMeasure", "onMeasure width = " + width);

		if (widthMode != MeasureSpec.EXACTLY) {

			throw new IllegalStateException(
					"ScrollLayout only canmCurScreen run at EXACTLY mode!");

		}

		final int heightMode = MeasureSpec.getMode(heightMeasureSpec);

		if (heightMode != MeasureSpec.EXACTLY) {

			// throw new IllegalStateException(
			// "ScrollLayout only can run at EXACTLY mode!");

		}

		// The children are given the same width and height as the scrollLayout

		final int count = getChildCount();

		for (int i = 0; i < count; i++) {

			getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);

		}

	}

	private SimpleOnGestureListener gestureListener = new SimpleOnGestureListener() {

		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			setMWidth();
			// Log.i("========onScroll111111================", distanceX);
			// if(Math.abs(distanceX) > 20){
			// return true;
			// }
			// ��Ҫ�ƶ��ľ���
			int dx = (int) distanceX;
			if (dx == 0) {
				return true;
			}
			// if("0".equals(flg)){
			// scrollBy(count1, 0);
			// }else{
			// count=0;
			// count1=0;
			// flg ="1";
			// } // �����ǰ��λ��ƫ����?
			int scrollX = getScrollX();

			// �ƶ�������λ��ƫ����
			int resultScrollX = scrollX + dx;
			final int destScreen = (getScrollX() + mWidth / 2) / mWidth;
			// ����
			if (dx >= 0) {
				int maxDestScreen = 0;
				if (destScreen == 3) {
					maxDestScreen = destScreen;
				} else {
					maxDestScreen = destScreen + 1;
				}

				Log.i("=============maxDestScreen================", ""
						+ maxDestScreen + "====........");

				if (resultScrollX >= (maxDestScreen) * mWidth) {
					//
					dx = (maxDestScreen) * mWidth - scrollX;
					Log.i("=============resultScrollX================", ""
							+ mWidth + "====........");
				}
				// ����
			} else {
				if (resultScrollX <= 0) {
					dx = -scrollX;
					Log.i("=============resultScrollX================", ""
							+ mWidth + "====........");
				}

			}

			fling = false;
			scrollBy(dx, 0);
			return true;
		}

		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			flg = "0";
			return true;

		}
	};

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		flg = "0";
		gestureDetector.onTouchEvent(ev);
		// scroll.onTouchEvent(ev);
		super.dispatchTouchEvent(ev);

		Log.i("==========onTouchEvent===============", "getScrollX=="
				+ getScrollX() + "destScreen==");

		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			break;
		case MotionEvent.ACTION_MOVE:

			break;
		case MotionEvent.ACTION_UP:
			count = 0;
			count1 = 0;
			Log.i("==========ACTION_UPfling00000000000===============",
					"getScrollX==" + getScrollX() + "fling==" + fling);

			Log.i("==========ACTION_UPfling===============", "getScrollX=="
					+ getScrollX() + "moveWidth==" + moveWidth);

			// �����ǰ��λ��ƫ����?
			int scrollX = getScrollX();
			int startX = 0;
			// ����ƫ����
			int dx = 0;
			mWidth = getWidth();
			final int destScreen = (getScrollX() + mWidth / 2) / mWidth;

			dx = (destScreen) * getWidth() - scrollX;
			startX = scrollX;
			Log.i("==========ACTION_UP====================", "getScrollX=="
					+ getScrollX() + "destScreen==" + dx);

			cmScroller.startScroll(startX, 0, dx, 0, Math.abs(dx) * 2);
			TestFlip.topImgAdapter.SetFocus(destScreen);
			aaa.translateAnimation(destScreen, destScreen, destScreen);
			postInvalidate();

			fling = false;
			break;
		default:
			break;
		}
		return true;

	}

	@Override
	public void computeScroll() {

		if (cmScroller.computeScrollOffset()) {
			scrollTo(cmScroller.getCurrX(), cmScroller.getCurrY());
			postInvalidate();
		}
	}

	public void skip(int i) {
		int curIndex = getScrollX() / getWidth();
		Log.i("==========skip===============", "getScrollX==" + getScrollX()
				+ "curIndex==" + curIndex);

		int dx = 0;
		int startX = 0;
		dx = i * getWidth() - getScrollX();
		startX = getScrollX();
		cmScroller.startScroll(startX, 0, dx, 0, Math.abs(dx) * 2);
		postInvalidate();

	}
}
